home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / grafik / 3d & render tools / irit / contrib / scripts / animbevl.irt < prev    next >
Encoding:
Text File  |  1996-07-16  |  5.2 KB  |  179 lines

  1. # ListCopy - deep copy of the lists
  2. # BsplineShowEval - returns list of polylines showing steps of the algorithm
  3. #                   at particular point
  4. # BsplineFindInterval - returns J such that t in [T_j, T_j+1)    
  5. # BsplineShowIntervals - returns list of curve pieces connection points
  6. # BsplineShowWithPolygon - returns list of objects: curve, control polygon,
  7. #                    control points and BsplineShowIntervals
  8. # BsplineShowAll - returns complete list of objects demonstrating algorithm
  9. #                  steps at specific parameter value t and views it
  10. # BsplineAnimateEval - animates BsplineShowAll at  parameter value samples
  11. #
  12. #                    Michael Plavnik 
  13. #
  14. #iritState("EchoSource", false);
  15.  
  16. order = 4;
  17.  
  18. ListCopy = FUNCTION( argList) : len  : i :
  19.   len = sizeof( argList ) :
  20.   return = NIL() :
  21.   FOR (i = 1, 1, len, snoc( nth( argList, i ), return ) );
  22.  
  23. BsplineShowEval = FUNCTION(k, polyList, knotList, J, t) 
  24. # returns list of polylines demonstrating steps of the algorithm at parameter t
  25.   : pList : pl : t_kpi : t_i : t_0 : t_1 : i : ii : p : colorValue  :
  26.   polyCurve : PtNew : td : Index :
  27.   pList = NIL() :
  28.   return = NIL() :
  29.   colorValue = 3 :
  30.   FOR(ii = J-k+1, 1, J, snoc( nth( polyList, ii+1 ), pList ) ) :
  31.   FOR(p = 1, 1, k-1,
  32.     pl = NIL() :
  33.     FOR(i = J-k+1+p, 1, J,
  34.       t_kpi = nth(knotList, i+k-p+1) :
  35.       t_i = nth(knotList, i+1) :
  36.       td = t_kpi -  t_i :
  37.       t_0 = (t - t_i) / ( t_kpi - t_i ) :
  38.       t_1 = 1 - t_0 :
  39.       Index = i-J+k-p :
  40.       PtNew = coerce(
  41.               t_0 * coerce(nth(pList, Index + 1 ), VECTOR_TYPE) + 
  42.               t_1 * coerce(nth(pList, Index ), VECTOR_TYPE),
  43.               E2) :
  44.       snoc( PtNew, pl ) 
  45.     ) :
  46.     color( pl, colorValue ) :
  47.     snoc(pl, return) :
  48.     IF (sizeof(pl) > 1,
  49.       polyCurve = cbspline(2, pl, list(KV_OPEN)) :
  50.       color( polyCurve, colorValue ) :
  51.       colorValue = colorValue + 1 :
  52.       snoc( polyCurve, return )
  53.     ) :
  54.     pList = ListCopy( pl )
  55.   );
  56.  
  57. BsplineFindInterval = FUNCTION(order, knotList, t)
  58. # returns J such that t in [T_j, T_j+1), where T_j stands for knotList[j]
  59.   : i : len :
  60.   return = 0 :
  61.   len = sizeof(knotList) - order - 1 :
  62.   FOR (i = order-1, 1, len,
  63.     IF (t >= nth(knotList, i+1),
  64.     return = i) );
  65.  
  66. BsplineShowIntervals = FUNCTION(order, polyList, knotList, splineCurve)
  67. # return list of connection points between pieces
  68.   : piece_list : i :
  69.   piece_list = NIL() :
  70.   FOR(i = 1, 1, sizeof(polyList)-1,
  71.     snoc( ceval(splineCurve, nth(knotList, i+order-1)), piece_list ) 
  72.   ) :
  73.   color(piece_list, 10) :
  74.   return = piece_list ;
  75.  
  76. BsplineShowWithPolygon = FUNCTION(k, polyList, knotList)
  77. # returns list of curve, its polygon and polygon points visualy
  78.   : spline_curve : polygon_curve :
  79.   spline_curve = cbspline(k, polyList, knotList ) :
  80.   polygon_curve = cbspline(2, polyList, list( KV_OPEN ) ) :
  81.   color( spline_curve, YELLOW ) :
  82.   color( polygon_curve, MAGENTA ) :
  83.   color( polyList, MAGENTA ) :
  84.   return = list( spline_curve, polygon_curve, polyList ) +
  85.        BsplineShowIntervals(k, polyList, knotList, spline_curve);
  86.  
  87. BsplineShowAll = FUNCTION(k, polyList, knotList, t)
  88.   : J :
  89.   J = BsplineFindInterval(k, knotList, t) :
  90.   return = list(
  91.          BsplineShowWithPolygon(order, polyList, knotList),
  92.              BsplineShowEval(order, polyList, knotList, J, t)
  93.            ) : 
  94.   view( return, TRUE );
  95.  
  96. BsplineAnimateEval = PROCEDURE(resolution, k, polyList, knotList)
  97.   : i : step : t : J :
  98.   FOR (J = order-1, 1, sizeof(polyList)-1,
  99.     t = nth(knotList, J+1) :
  100.     step = (nth(knotList, J+2) - t) / resolution : 
  101.     FOR (i = 1, 1, resolution,
  102.       view( list( BsplineShowWithPolygon(order, polyList, knotList),
  103.                 BsplineShowEval(order, polyList, knotList, J, t)
  104.                 ), 
  105.             TRUE 
  106.       ) :
  107.       printf("J = %g t = %f\\n", list( J, t) ) :
  108.       t = t + step  
  109.    ) 
  110.   );
  111.  
  112. iritState("EchoSource", true);
  113.  
  114. #
  115. # Set the view state
  116. #
  117. view_mat = sc( 0.4 );
  118. viewobj( list( view_mat ) );
  119. viewstate( "WiderLns" );
  120.  
  121. #
  122. # example 8.1
  123. #
  124. order = 4;
  125. knot_list = list( 0, 0, 0, 0, 1, 4, 5, 5, 5, 5 );
  126. polygon_list = list( ctlpt( E2, -1, -2),
  127.              ctlpt( E2, -2, -1),
  128.              ctlpt( E2, -1,  1),
  129.              ctlpt( E2,  1,  1),
  130.              ctlpt( E2,  2, -1), 
  131.              ctlpt( E2,  2,  0) );
  132.  
  133. BsplineShowAll(order, polygon_list, knot_list, 2);
  134. pause();
  135.  
  136. BsplineAnimateEval( 40, order, polygon_list, knot_list );
  137. BsplineAnimateEval( 40, order, polygon_list, knot_list );
  138. pause();
  139.  
  140. #
  141. # example 8.2
  142. #
  143. order = 3;
  144. knot_list = list( 0, 0, 0, 1, 3, 4, 4, 4 );
  145. polygon_list = list( ctlpt( E2, -1, -2),
  146.              ctlpt( E2, -2, -1),
  147.              ctlpt( E2, -1,  1),
  148.              ctlpt( E2,  1,  1),
  149.              ctlpt( E2,  2,  0) );
  150.  
  151.  
  152. BsplineShowAll( order, polygon_list, knot_list, 0.7);
  153. pause();
  154.  
  155. BsplineShowAll( order, polygon_list, knot_list, 1.7);
  156. pause();
  157.  
  158. BsplineAnimateEval( 40, order, polygon_list, knot_list );
  159. BsplineAnimateEval( 40, order, polygon_list, knot_list );
  160. pause();
  161.  
  162. #
  163. # Bezier case
  164. #
  165. order = 5;
  166. knot_list = list( 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 );
  167. polygon_list = list( ctlpt( E2, -1, -2),
  168.              ctlpt( E2, -2, -1),
  169.              ctlpt( E2, -1,  1),
  170.              ctlpt( E2,  1,  1),
  171.              ctlpt( E2,  2,  0) );
  172.  
  173. BsplineShowAll( order, polygon_list, knot_list, 0.35 );
  174. pause();
  175.  
  176. BsplineAnimateEval( 70, order, polygon_list, knot_list );
  177. BsplineAnimateEval( 70, order, polygon_list, knot_list );
  178. pause();
  179.